/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.exceptions.IllegalOrphanException;
import dao.exceptions.NonexistentEntityException;
import dao.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import pojos.Employees;
import pojos.JobsTitles;
import pojos.Currencies;
import pojos.SourceCode;
import java.util.ArrayList;
import java.util.Collection;
import pojos.Projects;
import pojos.TimeLog;
import pojos.Expenses;

/**
 *
 * @author A7med Kotb
 */
public class EmployeesJpaController implements Serializable {

    public EmployeesJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Employees employees) throws RollbackFailureException, Exception {
        if (employees.getSourceCodeCollection() == null) {
            employees.setSourceCodeCollection(new ArrayList<SourceCode>());
        }
        if (employees.getProjectsCollection() == null) {
            employees.setProjectsCollection(new ArrayList<Projects>());
        }
        if (employees.getTimeLogCollection() == null) {
            employees.setTimeLogCollection(new ArrayList<TimeLog>());
        }
        if (employees.getTimeLogCollection1() == null) {
            employees.setTimeLogCollection1(new ArrayList<TimeLog>());
        }
        if (employees.getExpensesCollection() == null) {
            employees.setExpensesCollection(new ArrayList<Expenses>());
        }
        if (employees.getExpensesCollection1() == null) {
            employees.setExpensesCollection1(new ArrayList<Expenses>());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            JobsTitles jobTitle2 = employees.getJobTitle2();
            if (jobTitle2 != null) {
                jobTitle2 = em.getReference(jobTitle2.getClass(), jobTitle2.getJobTitleId());
                employees.setJobTitle2(jobTitle2);
            }
            JobsTitles jobTitle1 = employees.getJobTitle1();
            if (jobTitle1 != null) {
                jobTitle1 = em.getReference(jobTitle1.getClass(), jobTitle1.getJobTitleId());
                employees.setJobTitle1(jobTitle1);
            }
            Currencies currency = employees.getCurrency();
            if (currency != null) {
                currency = em.getReference(currency.getClass(), currency.getCurrId());
                employees.setCurrency(currency);
            }
            Collection<SourceCode> attachedSourceCodeCollection = new ArrayList<SourceCode>();
            for (SourceCode sourceCodeCollectionSourceCodeToAttach : employees.getSourceCodeCollection()) {
                sourceCodeCollectionSourceCodeToAttach = em.getReference(sourceCodeCollectionSourceCodeToAttach.getClass(), sourceCodeCollectionSourceCodeToAttach.getScId());
                attachedSourceCodeCollection.add(sourceCodeCollectionSourceCodeToAttach);
            }
            employees.setSourceCodeCollection(attachedSourceCodeCollection);
            Collection<Projects> attachedProjectsCollection = new ArrayList<Projects>();
            for (Projects projectsCollectionProjectsToAttach : employees.getProjectsCollection()) {
                projectsCollectionProjectsToAttach = em.getReference(projectsCollectionProjectsToAttach.getClass(), projectsCollectionProjectsToAttach.getProId());
                attachedProjectsCollection.add(projectsCollectionProjectsToAttach);
            }
            employees.setProjectsCollection(attachedProjectsCollection);
            Collection<TimeLog> attachedTimeLogCollection = new ArrayList<TimeLog>();
            for (TimeLog timeLogCollectionTimeLogToAttach : employees.getTimeLogCollection()) {
                timeLogCollectionTimeLogToAttach = em.getReference(timeLogCollectionTimeLogToAttach.getClass(), timeLogCollectionTimeLogToAttach.getTlId());
                attachedTimeLogCollection.add(timeLogCollectionTimeLogToAttach);
            }
            employees.setTimeLogCollection(attachedTimeLogCollection);
            Collection<TimeLog> attachedTimeLogCollection1 = new ArrayList<TimeLog>();
            for (TimeLog timeLogCollection1TimeLogToAttach : employees.getTimeLogCollection1()) {
                timeLogCollection1TimeLogToAttach = em.getReference(timeLogCollection1TimeLogToAttach.getClass(), timeLogCollection1TimeLogToAttach.getTlId());
                attachedTimeLogCollection1.add(timeLogCollection1TimeLogToAttach);
            }
            employees.setTimeLogCollection1(attachedTimeLogCollection1);
            Collection<Expenses> attachedExpensesCollection = new ArrayList<Expenses>();
            for (Expenses expensesCollectionExpensesToAttach : employees.getExpensesCollection()) {
                expensesCollectionExpensesToAttach = em.getReference(expensesCollectionExpensesToAttach.getClass(), expensesCollectionExpensesToAttach.getExpId());
                attachedExpensesCollection.add(expensesCollectionExpensesToAttach);
            }
            employees.setExpensesCollection(attachedExpensesCollection);
            Collection<Expenses> attachedExpensesCollection1 = new ArrayList<Expenses>();
            for (Expenses expensesCollection1ExpensesToAttach : employees.getExpensesCollection1()) {
                expensesCollection1ExpensesToAttach = em.getReference(expensesCollection1ExpensesToAttach.getClass(), expensesCollection1ExpensesToAttach.getExpId());
                attachedExpensesCollection1.add(expensesCollection1ExpensesToAttach);
            }
            employees.setExpensesCollection1(attachedExpensesCollection1);
            em.persist(employees);
            if (jobTitle2 != null) {
                jobTitle2.getEmployeesCollection().add(employees);
                jobTitle2 = em.merge(jobTitle2);
            }
            if (jobTitle1 != null) {
                jobTitle1.getEmployeesCollection().add(employees);
                jobTitle1 = em.merge(jobTitle1);
            }
            if (currency != null) {
                currency.getEmployeesCollection().add(employees);
                currency = em.merge(currency);
            }
            for (SourceCode sourceCodeCollectionSourceCode : employees.getSourceCodeCollection()) {
                Employees oldEmployeeOfSourceCodeCollectionSourceCode = sourceCodeCollectionSourceCode.getEmployee();
                sourceCodeCollectionSourceCode.setEmployee(employees);
                sourceCodeCollectionSourceCode = em.merge(sourceCodeCollectionSourceCode);
                if (oldEmployeeOfSourceCodeCollectionSourceCode != null) {
                    oldEmployeeOfSourceCodeCollectionSourceCode.getSourceCodeCollection().remove(sourceCodeCollectionSourceCode);
                    oldEmployeeOfSourceCodeCollectionSourceCode = em.merge(oldEmployeeOfSourceCodeCollectionSourceCode);
                }
            }
            for (Projects projectsCollectionProjects : employees.getProjectsCollection()) {
                Employees oldCreatedByOfProjectsCollectionProjects = projectsCollectionProjects.getCreatedBy();
                projectsCollectionProjects.setCreatedBy(employees);
                projectsCollectionProjects = em.merge(projectsCollectionProjects);
                if (oldCreatedByOfProjectsCollectionProjects != null) {
                    oldCreatedByOfProjectsCollectionProjects.getProjectsCollection().remove(projectsCollectionProjects);
                    oldCreatedByOfProjectsCollectionProjects = em.merge(oldCreatedByOfProjectsCollectionProjects);
                }
            }
            for (TimeLog timeLogCollectionTimeLog : employees.getTimeLogCollection()) {
                Employees oldEmplyeeOfTimeLogCollectionTimeLog = timeLogCollectionTimeLog.getEmplyee();
                timeLogCollectionTimeLog.setEmplyee(employees);
                timeLogCollectionTimeLog = em.merge(timeLogCollectionTimeLog);
                if (oldEmplyeeOfTimeLogCollectionTimeLog != null) {
                    oldEmplyeeOfTimeLogCollectionTimeLog.getTimeLogCollection().remove(timeLogCollectionTimeLog);
                    oldEmplyeeOfTimeLogCollectionTimeLog = em.merge(oldEmplyeeOfTimeLogCollectionTimeLog);
                }
            }
            for (TimeLog timeLogCollection1TimeLog : employees.getTimeLogCollection1()) {
                Employees oldCreatedByOfTimeLogCollection1TimeLog = timeLogCollection1TimeLog.getCreatedBy();
                timeLogCollection1TimeLog.setCreatedBy(employees);
                timeLogCollection1TimeLog = em.merge(timeLogCollection1TimeLog);
                if (oldCreatedByOfTimeLogCollection1TimeLog != null) {
                    oldCreatedByOfTimeLogCollection1TimeLog.getTimeLogCollection1().remove(timeLogCollection1TimeLog);
                    oldCreatedByOfTimeLogCollection1TimeLog = em.merge(oldCreatedByOfTimeLogCollection1TimeLog);
                }
            }
            for (Expenses expensesCollectionExpenses : employees.getExpensesCollection()) {
                Employees oldEmployeeOfExpensesCollectionExpenses = expensesCollectionExpenses.getEmployee();
                expensesCollectionExpenses.setEmployee(employees);
                expensesCollectionExpenses = em.merge(expensesCollectionExpenses);
                if (oldEmployeeOfExpensesCollectionExpenses != null) {
                    oldEmployeeOfExpensesCollectionExpenses.getExpensesCollection().remove(expensesCollectionExpenses);
                    oldEmployeeOfExpensesCollectionExpenses = em.merge(oldEmployeeOfExpensesCollectionExpenses);
                }
            }
            for (Expenses expensesCollection1Expenses : employees.getExpensesCollection1()) {
                Employees oldCreatedByOfExpensesCollection1Expenses = expensesCollection1Expenses.getCreatedBy();
                expensesCollection1Expenses.setCreatedBy(employees);
                expensesCollection1Expenses = em.merge(expensesCollection1Expenses);
                if (oldCreatedByOfExpensesCollection1Expenses != null) {
                    oldCreatedByOfExpensesCollection1Expenses.getExpensesCollection1().remove(expensesCollection1Expenses);
                    oldCreatedByOfExpensesCollection1Expenses = em.merge(oldCreatedByOfExpensesCollection1Expenses);
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Employees employees) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Employees persistentEmployees = em.find(Employees.class, employees.getEmpId());
            JobsTitles jobTitle2Old = persistentEmployees.getJobTitle2();
            JobsTitles jobTitle2New = employees.getJobTitle2();
            JobsTitles jobTitle1Old = persistentEmployees.getJobTitle1();
            JobsTitles jobTitle1New = employees.getJobTitle1();
            Currencies currencyOld = persistentEmployees.getCurrency();
            Currencies currencyNew = employees.getCurrency();
            Collection<SourceCode> sourceCodeCollectionOld = persistentEmployees.getSourceCodeCollection();
            Collection<SourceCode> sourceCodeCollectionNew = employees.getSourceCodeCollection();
            Collection<Projects> projectsCollectionOld = persistentEmployees.getProjectsCollection();
            Collection<Projects> projectsCollectionNew = employees.getProjectsCollection();
            Collection<TimeLog> timeLogCollectionOld = persistentEmployees.getTimeLogCollection();
            Collection<TimeLog> timeLogCollectionNew = employees.getTimeLogCollection();
            Collection<TimeLog> timeLogCollection1Old = persistentEmployees.getTimeLogCollection1();
            Collection<TimeLog> timeLogCollection1New = employees.getTimeLogCollection1();
            Collection<Expenses> expensesCollectionOld = persistentEmployees.getExpensesCollection();
            Collection<Expenses> expensesCollectionNew = employees.getExpensesCollection();
            Collection<Expenses> expensesCollection1Old = persistentEmployees.getExpensesCollection1();
            Collection<Expenses> expensesCollection1New = employees.getExpensesCollection1();
            List<String> illegalOrphanMessages = null;
            for (SourceCode sourceCodeCollectionOldSourceCode : sourceCodeCollectionOld) {
                if (!sourceCodeCollectionNew.contains(sourceCodeCollectionOldSourceCode)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain SourceCode " + sourceCodeCollectionOldSourceCode + " since its employee field is not nullable.");
                }
            }
            for (Projects projectsCollectionOldProjects : projectsCollectionOld) {
                if (!projectsCollectionNew.contains(projectsCollectionOldProjects)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Projects " + projectsCollectionOldProjects + " since its createdBy field is not nullable.");
                }
            }
            for (TimeLog timeLogCollectionOldTimeLog : timeLogCollectionOld) {
                if (!timeLogCollectionNew.contains(timeLogCollectionOldTimeLog)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain TimeLog " + timeLogCollectionOldTimeLog + " since its emplyee field is not nullable.");
                }
            }
            for (TimeLog timeLogCollection1OldTimeLog : timeLogCollection1Old) {
                if (!timeLogCollection1New.contains(timeLogCollection1OldTimeLog)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain TimeLog " + timeLogCollection1OldTimeLog + " since its createdBy field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (jobTitle2New != null) {
                jobTitle2New = em.getReference(jobTitle2New.getClass(), jobTitle2New.getJobTitleId());
                employees.setJobTitle2(jobTitle2New);
            }
            if (jobTitle1New != null) {
                jobTitle1New = em.getReference(jobTitle1New.getClass(), jobTitle1New.getJobTitleId());
                employees.setJobTitle1(jobTitle1New);
            }
            if (currencyNew != null) {
                currencyNew = em.getReference(currencyNew.getClass(), currencyNew.getCurrId());
                employees.setCurrency(currencyNew);
            }
            Collection<SourceCode> attachedSourceCodeCollectionNew = new ArrayList<SourceCode>();
            for (SourceCode sourceCodeCollectionNewSourceCodeToAttach : sourceCodeCollectionNew) {
                sourceCodeCollectionNewSourceCodeToAttach = em.getReference(sourceCodeCollectionNewSourceCodeToAttach.getClass(), sourceCodeCollectionNewSourceCodeToAttach.getScId());
                attachedSourceCodeCollectionNew.add(sourceCodeCollectionNewSourceCodeToAttach);
            }
            sourceCodeCollectionNew = attachedSourceCodeCollectionNew;
            employees.setSourceCodeCollection(sourceCodeCollectionNew);
            Collection<Projects> attachedProjectsCollectionNew = new ArrayList<Projects>();
            for (Projects projectsCollectionNewProjectsToAttach : projectsCollectionNew) {
                projectsCollectionNewProjectsToAttach = em.getReference(projectsCollectionNewProjectsToAttach.getClass(), projectsCollectionNewProjectsToAttach.getProId());
                attachedProjectsCollectionNew.add(projectsCollectionNewProjectsToAttach);
            }
            projectsCollectionNew = attachedProjectsCollectionNew;
            employees.setProjectsCollection(projectsCollectionNew);
            Collection<TimeLog> attachedTimeLogCollectionNew = new ArrayList<TimeLog>();
            for (TimeLog timeLogCollectionNewTimeLogToAttach : timeLogCollectionNew) {
                timeLogCollectionNewTimeLogToAttach = em.getReference(timeLogCollectionNewTimeLogToAttach.getClass(), timeLogCollectionNewTimeLogToAttach.getTlId());
                attachedTimeLogCollectionNew.add(timeLogCollectionNewTimeLogToAttach);
            }
            timeLogCollectionNew = attachedTimeLogCollectionNew;
            employees.setTimeLogCollection(timeLogCollectionNew);
            Collection<TimeLog> attachedTimeLogCollection1New = new ArrayList<TimeLog>();
            for (TimeLog timeLogCollection1NewTimeLogToAttach : timeLogCollection1New) {
                timeLogCollection1NewTimeLogToAttach = em.getReference(timeLogCollection1NewTimeLogToAttach.getClass(), timeLogCollection1NewTimeLogToAttach.getTlId());
                attachedTimeLogCollection1New.add(timeLogCollection1NewTimeLogToAttach);
            }
            timeLogCollection1New = attachedTimeLogCollection1New;
            employees.setTimeLogCollection1(timeLogCollection1New);
            Collection<Expenses> attachedExpensesCollectionNew = new ArrayList<Expenses>();
            for (Expenses expensesCollectionNewExpensesToAttach : expensesCollectionNew) {
                expensesCollectionNewExpensesToAttach = em.getReference(expensesCollectionNewExpensesToAttach.getClass(), expensesCollectionNewExpensesToAttach.getExpId());
                attachedExpensesCollectionNew.add(expensesCollectionNewExpensesToAttach);
            }
            expensesCollectionNew = attachedExpensesCollectionNew;
            employees.setExpensesCollection(expensesCollectionNew);
            Collection<Expenses> attachedExpensesCollection1New = new ArrayList<Expenses>();
            for (Expenses expensesCollection1NewExpensesToAttach : expensesCollection1New) {
                expensesCollection1NewExpensesToAttach = em.getReference(expensesCollection1NewExpensesToAttach.getClass(), expensesCollection1NewExpensesToAttach.getExpId());
                attachedExpensesCollection1New.add(expensesCollection1NewExpensesToAttach);
            }
            expensesCollection1New = attachedExpensesCollection1New;
            employees.setExpensesCollection1(expensesCollection1New);
            employees = em.merge(employees);
            if (jobTitle2Old != null && !jobTitle2Old.equals(jobTitle2New)) {
                jobTitle2Old.getEmployeesCollection().remove(employees);
                jobTitle2Old = em.merge(jobTitle2Old);
            }
            if (jobTitle2New != null && !jobTitle2New.equals(jobTitle2Old)) {
                jobTitle2New.getEmployeesCollection().add(employees);
                jobTitle2New = em.merge(jobTitle2New);
            }
            if (jobTitle1Old != null && !jobTitle1Old.equals(jobTitle1New)) {
                jobTitle1Old.getEmployeesCollection().remove(employees);
                jobTitle1Old = em.merge(jobTitle1Old);
            }
            if (jobTitle1New != null && !jobTitle1New.equals(jobTitle1Old)) {
                jobTitle1New.getEmployeesCollection().add(employees);
                jobTitle1New = em.merge(jobTitle1New);
            }
            if (currencyOld != null && !currencyOld.equals(currencyNew)) {
                currencyOld.getEmployeesCollection().remove(employees);
                currencyOld = em.merge(currencyOld);
            }
            if (currencyNew != null && !currencyNew.equals(currencyOld)) {
                currencyNew.getEmployeesCollection().add(employees);
                currencyNew = em.merge(currencyNew);
            }
            for (SourceCode sourceCodeCollectionNewSourceCode : sourceCodeCollectionNew) {
                if (!sourceCodeCollectionOld.contains(sourceCodeCollectionNewSourceCode)) {
                    Employees oldEmployeeOfSourceCodeCollectionNewSourceCode = sourceCodeCollectionNewSourceCode.getEmployee();
                    sourceCodeCollectionNewSourceCode.setEmployee(employees);
                    sourceCodeCollectionNewSourceCode = em.merge(sourceCodeCollectionNewSourceCode);
                    if (oldEmployeeOfSourceCodeCollectionNewSourceCode != null && !oldEmployeeOfSourceCodeCollectionNewSourceCode.equals(employees)) {
                        oldEmployeeOfSourceCodeCollectionNewSourceCode.getSourceCodeCollection().remove(sourceCodeCollectionNewSourceCode);
                        oldEmployeeOfSourceCodeCollectionNewSourceCode = em.merge(oldEmployeeOfSourceCodeCollectionNewSourceCode);
                    }
                }
            }
            for (Projects projectsCollectionNewProjects : projectsCollectionNew) {
                if (!projectsCollectionOld.contains(projectsCollectionNewProjects)) {
                    Employees oldCreatedByOfProjectsCollectionNewProjects = projectsCollectionNewProjects.getCreatedBy();
                    projectsCollectionNewProjects.setCreatedBy(employees);
                    projectsCollectionNewProjects = em.merge(projectsCollectionNewProjects);
                    if (oldCreatedByOfProjectsCollectionNewProjects != null && !oldCreatedByOfProjectsCollectionNewProjects.equals(employees)) {
                        oldCreatedByOfProjectsCollectionNewProjects.getProjectsCollection().remove(projectsCollectionNewProjects);
                        oldCreatedByOfProjectsCollectionNewProjects = em.merge(oldCreatedByOfProjectsCollectionNewProjects);
                    }
                }
            }
            for (TimeLog timeLogCollectionNewTimeLog : timeLogCollectionNew) {
                if (!timeLogCollectionOld.contains(timeLogCollectionNewTimeLog)) {
                    Employees oldEmplyeeOfTimeLogCollectionNewTimeLog = timeLogCollectionNewTimeLog.getEmplyee();
                    timeLogCollectionNewTimeLog.setEmplyee(employees);
                    timeLogCollectionNewTimeLog = em.merge(timeLogCollectionNewTimeLog);
                    if (oldEmplyeeOfTimeLogCollectionNewTimeLog != null && !oldEmplyeeOfTimeLogCollectionNewTimeLog.equals(employees)) {
                        oldEmplyeeOfTimeLogCollectionNewTimeLog.getTimeLogCollection().remove(timeLogCollectionNewTimeLog);
                        oldEmplyeeOfTimeLogCollectionNewTimeLog = em.merge(oldEmplyeeOfTimeLogCollectionNewTimeLog);
                    }
                }
            }
            for (TimeLog timeLogCollection1NewTimeLog : timeLogCollection1New) {
                if (!timeLogCollection1Old.contains(timeLogCollection1NewTimeLog)) {
                    Employees oldCreatedByOfTimeLogCollection1NewTimeLog = timeLogCollection1NewTimeLog.getCreatedBy();
                    timeLogCollection1NewTimeLog.setCreatedBy(employees);
                    timeLogCollection1NewTimeLog = em.merge(timeLogCollection1NewTimeLog);
                    if (oldCreatedByOfTimeLogCollection1NewTimeLog != null && !oldCreatedByOfTimeLogCollection1NewTimeLog.equals(employees)) {
                        oldCreatedByOfTimeLogCollection1NewTimeLog.getTimeLogCollection1().remove(timeLogCollection1NewTimeLog);
                        oldCreatedByOfTimeLogCollection1NewTimeLog = em.merge(oldCreatedByOfTimeLogCollection1NewTimeLog);
                    }
                }
            }
            for (Expenses expensesCollectionOldExpenses : expensesCollectionOld) {
                if (!expensesCollectionNew.contains(expensesCollectionOldExpenses)) {
                    expensesCollectionOldExpenses.setEmployee(null);
                    expensesCollectionOldExpenses = em.merge(expensesCollectionOldExpenses);
                }
            }
            for (Expenses expensesCollectionNewExpenses : expensesCollectionNew) {
                if (!expensesCollectionOld.contains(expensesCollectionNewExpenses)) {
                    Employees oldEmployeeOfExpensesCollectionNewExpenses = expensesCollectionNewExpenses.getEmployee();
                    expensesCollectionNewExpenses.setEmployee(employees);
                    expensesCollectionNewExpenses = em.merge(expensesCollectionNewExpenses);
                    if (oldEmployeeOfExpensesCollectionNewExpenses != null && !oldEmployeeOfExpensesCollectionNewExpenses.equals(employees)) {
                        oldEmployeeOfExpensesCollectionNewExpenses.getExpensesCollection().remove(expensesCollectionNewExpenses);
                        oldEmployeeOfExpensesCollectionNewExpenses = em.merge(oldEmployeeOfExpensesCollectionNewExpenses);
                    }
                }
            }
            for (Expenses expensesCollection1OldExpenses : expensesCollection1Old) {
                if (!expensesCollection1New.contains(expensesCollection1OldExpenses)) {
                    expensesCollection1OldExpenses.setCreatedBy(null);
                    expensesCollection1OldExpenses = em.merge(expensesCollection1OldExpenses);
                }
            }
            for (Expenses expensesCollection1NewExpenses : expensesCollection1New) {
                if (!expensesCollection1Old.contains(expensesCollection1NewExpenses)) {
                    Employees oldCreatedByOfExpensesCollection1NewExpenses = expensesCollection1NewExpenses.getCreatedBy();
                    expensesCollection1NewExpenses.setCreatedBy(employees);
                    expensesCollection1NewExpenses = em.merge(expensesCollection1NewExpenses);
                    if (oldCreatedByOfExpensesCollection1NewExpenses != null && !oldCreatedByOfExpensesCollection1NewExpenses.equals(employees)) {
                        oldCreatedByOfExpensesCollection1NewExpenses.getExpensesCollection1().remove(expensesCollection1NewExpenses);
                        oldCreatedByOfExpensesCollection1NewExpenses = em.merge(oldCreatedByOfExpensesCollection1NewExpenses);
                    }
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = employees.getEmpId();
                if (findEmployees(id) == null) {
                    throw new NonexistentEntityException("The employees with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Employees employees;
            try {
                employees = em.getReference(Employees.class, id);
                employees.getEmpId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The employees with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<SourceCode> sourceCodeCollectionOrphanCheck = employees.getSourceCodeCollection();
            for (SourceCode sourceCodeCollectionOrphanCheckSourceCode : sourceCodeCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Employees (" + employees + ") cannot be destroyed since the SourceCode " + sourceCodeCollectionOrphanCheckSourceCode + " in its sourceCodeCollection field has a non-nullable employee field.");
            }
            Collection<Projects> projectsCollectionOrphanCheck = employees.getProjectsCollection();
            for (Projects projectsCollectionOrphanCheckProjects : projectsCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Employees (" + employees + ") cannot be destroyed since the Projects " + projectsCollectionOrphanCheckProjects + " in its projectsCollection field has a non-nullable createdBy field.");
            }
            Collection<TimeLog> timeLogCollectionOrphanCheck = employees.getTimeLogCollection();
            for (TimeLog timeLogCollectionOrphanCheckTimeLog : timeLogCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Employees (" + employees + ") cannot be destroyed since the TimeLog " + timeLogCollectionOrphanCheckTimeLog + " in its timeLogCollection field has a non-nullable emplyee field.");
            }
            Collection<TimeLog> timeLogCollection1OrphanCheck = employees.getTimeLogCollection1();
            for (TimeLog timeLogCollection1OrphanCheckTimeLog : timeLogCollection1OrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Employees (" + employees + ") cannot be destroyed since the TimeLog " + timeLogCollection1OrphanCheckTimeLog + " in its timeLogCollection1 field has a non-nullable createdBy field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            JobsTitles jobTitle2 = employees.getJobTitle2();
            if (jobTitle2 != null) {
                jobTitle2.getEmployeesCollection().remove(employees);
                jobTitle2 = em.merge(jobTitle2);
            }
            JobsTitles jobTitle1 = employees.getJobTitle1();
            if (jobTitle1 != null) {
                jobTitle1.getEmployeesCollection().remove(employees);
                jobTitle1 = em.merge(jobTitle1);
            }
            Currencies currency = employees.getCurrency();
            if (currency != null) {
                currency.getEmployeesCollection().remove(employees);
                currency = em.merge(currency);
            }
            Collection<Expenses> expensesCollection = employees.getExpensesCollection();
            for (Expenses expensesCollectionExpenses : expensesCollection) {
                expensesCollectionExpenses.setEmployee(null);
                expensesCollectionExpenses = em.merge(expensesCollectionExpenses);
            }
            Collection<Expenses> expensesCollection1 = employees.getExpensesCollection1();
            for (Expenses expensesCollection1Expenses : expensesCollection1) {
                expensesCollection1Expenses.setCreatedBy(null);
                expensesCollection1Expenses = em.merge(expensesCollection1Expenses);
            }
            em.remove(employees);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Employees> findEmployeesEntities() {
        return findEmployeesEntities(true, -1, -1);
    }

    public List<Employees> findEmployeesEntities(int maxResults, int firstResult) {
        return findEmployeesEntities(false, maxResults, firstResult);
    }

    private List<Employees> findEmployeesEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Employees.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Employees findEmployees(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Employees.class, id);
        } finally {
            em.close();
        }
    }

    public int getEmployeesCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Employees> rt = cq.from(Employees.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
